package com.htfyun.ft.rooboUtils;

import android.content.Context;

import com.iflytek.cloud.util.ResourceUtil;

/**
 * Created by yuanshuqi on 2017/2/22 10:20.
 * email:yuanshuqi@roo.bo
 */
public class RooboEngine implements RooboRecorder.OnReadDataListener {

    private final int WAKEUP_SCORE = -15;

    private Context mContext;
    private XunFeiWakeUpTranslator mTranslator;
    private RooboRecorder rooboRecorder;

    private volatile boolean inited = false;
    public RooboEngine(Context context) {
        mContext = context;
    }

    public void init() {

        if (inited) {
            return;
        }
        inited = true;

        mTranslator = new XunFeiWakeUpTranslator(ResourceUtil.generateResourcePath(mContext,
                ResourceUtil.RESOURCE_TYPE.assets, "ivw/552cd878.jet"), mContext);
        mTranslator.init();

        rooboRecorder = new RooboRecorder(this);
        rooboRecorder.startRecord();
    }

    public void deinit() {

        if (rooboRecorder != null) {
            rooboRecorder.destroyRecorder();
            rooboRecorder = null;
        }

        if (mTranslator != null) {
            mTranslator.destory();
            mTranslator = null;
        }

    }

    private void produceData(byte[] data) {
        if (mTranslator == null) {
            return;
        }
        mTranslator.translateVoice(data);
    }

    public void setWakeupListener(XunFeiWakeUpTranslator.WakeupListener listener) {
        if (mTranslator == null) {
            return ;
        }
        mTranslator.setWakeupListener(listener);
    }

    @Override
    public void onReadDataListener(byte[] data) {

        produceData(data);

    }
}
